#include "staff.h"

/*****************************************员工信息保存函数*****************************************************************/
// 从文件中加载用户到链表
void LoadStaffToList(NODE *pStaffList, const char *filename)
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return;
    }
    STAFF staff;
    while (fscanf(fp, "%s %s %s %s %s %s", staff.name, staff.sex, staff.age, staff.salary, staff.department, staff.change) == 6)
    {
        STAFF *newStaff = (STAFF *)malloc(sizeof(STAFF));
        strcpy(newStaff->name, staff.name);
        strcpy(newStaff->sex, staff.sex);
        strcpy(newStaff->age, staff.age);
        strcpy(newStaff->salary, staff.salary);
        strcpy(newStaff->department, staff.department);
        strcpy(newStaff->change, staff.change);
        tianJiaJieDian(pStaffList, newStaff);
    }
    fclose(fp);
}
// 保存所有用户信息用于修改
void SaveAllStaff(NODE *pStaffList, const char *filename)
{
    FILE *fp = fopen(filename, "w");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return;
    }
    NODE *p = pStaffList->xiayige; // 跳过头节点
    while (p != NULL)
    {
        STAFF *pStaff = (STAFF *)p->shuju;
        fprintf(fp, "%s %s %s %s %s %s\n", pStaff->name, pStaff->sex, pStaff->age, pStaff->salary, pStaff->department, pStaff->change);
        p = p->xiayige;
    }
    fclose(fp);
}
// 保存用户结构体
void SaveStaff(STAFF *pStaff, const char *filename)
{
    FILE *fp = fopen(filename, "aw");                                                                                               // 打开文件用来写入信息
    fprintf(fp, "%s %s %s %s %s %s\n", pStaff->name, pStaff->sex, pStaff->age, pStaff->salary, pStaff->department, pStaff->change); // 把用户结构体中的内容打印到文件中
    fclose(fp);
}

/*--------------------------------------------员工信息操作函数-----------------------------------------------------------*/
// 改变员工信息
void Change_message(NODE *pStaffList, const char *filename)
{
    char name[128];
    printf("请输入要修改的员工姓名：");
    scanf("%s", name);

    NODE *p = pStaffList->xiayige;
    while (p != NULL)
    {
        STAFF *staff = (STAFF *)p->shuju;
        if (strcmp(staff->name, name) == 0)
        {
            printf("请输入新的员工年龄：");
            scanf("%s", staff->age);
            printf("请输入新的员工的薪水：");
            scanf("%s", staff->salary);
            printf("输入新的员工部门：");
            scanf("%s", staff->department);
            SaveAllStaff(pStaffList, filename);
            printf("员工信息修改成功!\n");
            return;
        }
        p = p->xiayige;
    }
    printf("未找到要修改的员工信息!\n");
}
// 添加员工信息
void Add_message(NODE *pStaffList, const char *filename)
{
    STAFF *newStaff = (STAFF *)malloc(sizeof(STAFF));
    printf("请输入员工姓名: ");
    scanf("%s", newStaff->name);
    printf("请输入员工性别: ");
    scanf("%s", newStaff->sex);
    printf("请输入员工年龄: ");
    scanf("%s", newStaff->age);
    printf("请输入员工薪水: ");
    scanf("%s", newStaff->salary);
    printf("请输入员工部门: ");
    scanf("%s", newStaff->department);
    strcpy(newStaff->change, "空");

    NODE *newNode = (NODE *)malloc(sizeof(NODE));
    newNode->shuju = newStaff;
    newNode->xiayige = pStaffList->xiayige;
    pStaffList->xiayige = newNode;
    SaveAllStaff(pStaffList, filename);
    printf("员工信息添加成功!\n");
}
// 删除员工
void Delete_message(NODE *pStaffList, const char *filename)
{
    char name[128];
    printf("请输入要删除的员工姓名：");
    scanf("%s", name);

    NODE *p = pStaffList;
    while (p != NULL)
    {
        STAFF *staff = (STAFF *)p->xiayige->shuju;
        if (strcmp(staff->name, name) == 0) // 对比并删除
        {
            NODE *temp = p->xiayige;
            p->xiayige = p->xiayige->xiayige;
            free(temp->shuju);
            free(temp);
            SaveAllStaff(pStaffList, filename);
            printf("员工信息删除成功!\n");
            return;
        }
        p = p->xiayige;
    }
    printf("未找到要删除的员工信息!\n");
}
// 显示所有员工信息
void Show_message(NODE *pStaffList)
{
    NODE *p = pStaffList->xiayige;
    while (p != NULL)
    {
        STAFF *staff = (STAFF *)p->shuju;
        printf("姓名：%s,性别：%s,年龄：%s,薪水：%s,部门：%s,申请更换的部门：%s\n", staff->name, staff->sex, staff->age, staff->salary, staff->department, staff->change);
        p = p->xiayige;
    }
}
// 显示个人所有信息
void Show_person(NODE *pStaffList)
{
    char name[128];
    printf("输入员工姓名：");
    scanf("%s", name);
    NODE *p = pStaffList->xiayige;
    while (p != NULL)
    {
        STAFF *staff = (STAFF *)p->shuju;
        if (strcmp(staff->name, name) == 0)
        {
            printf("姓名：%s,性别：%s,年龄：%s,薪水：%s,部门：%s,申请更换的部门：%s\n", staff->name, staff->sex, staff->age, staff->salary, staff->department, staff->change);
            return;
        }
        p = p->xiayige;
    }
    printf("未找到该员工的信息!\n");
}
// 更改员工的部门根据员工申请的部门，并且把申请的部门一栏用“空”字表示已经更换
void Approve_apply(NODE *pStaffList, const char *filename)
{
    char name[128];
    printf("请输入要更改部门的员工姓名：");
    scanf("%s", name);

    NODE *p = pStaffList->xiayige;
    while (p != NULL)
    {
        STAFF *staff = (STAFF *)p->shuju;
        if (strcmp(staff->name, name) == 0)
        {
            if (strcmp(staff->change, "空") != 0)
            {
                strcpy(staff->department, staff->change);
                strcpy(staff->change, "空");
                SaveAllStaff(pStaffList, filename);
                printf("部门更改成功!\n");
            }
            else
            {
                printf("该员工没有申请更换部门!\n");
            }
            return;
        }
        p = p->xiayige;
    }
    printf("未找到该员工信息!\n");
}
// 查看工资
void Show_salary(NODE *pStaffList)
{
    char name[128];
    printf("请输入员工姓名：");
    scanf("%s", name);

    NODE *p = pStaffList->xiayige;
    while (p != NULL)
    {
        STAFF *staff = (STAFF *)p->shuju;
        if (strcmp(staff->name, name) == 0)
        {
            printf("姓名：%s,薪水：%s", staff->name, staff->salary);
        }
        p = p->xiayige;
    }
    printf("未找到该员工信息!\n");
}
// 查看所在部门
void Show_department(NODE *pStaffList)
{
    char name[128];
    printf("请输入员工姓名：");
    scanf("%s", name);

    NODE *p = pStaffList->xiayige;
    while (p != NULL)
    {
        STAFF *staff = (STAFF *)p->shuju;
        if (strcmp(staff->name, name) == 0)
        {
            printf("姓名：%s,部门：%s", staff->name, staff->department);
        }
        p = p->xiayige;
    }
    printf("未找到该员工信息!\n");
}
// 申请更换部门，初始为"空"字字符串,申请后就更换成要申请的部门字符串
void Apply_change_Dp(NODE *pStaffList, const char *filename)
{
    char name[128];
    printf("请输入你的姓名：");
    scanf("%s", name);
    NODE *p = pStaffList->xiayige;
    while (p != NULL)
    {
        STAFF *staff = (STAFF *)p->shuju;
        if (strcmp(staff->name, name) == 0)
        {
            printf("请输入申请更换的部门：");
            scanf("%s", staff->change);
            SaveAllStaff(pStaffList, filename);
            printf("申请成功!\n");
            return;
        }
        p = p->xiayige;
    }
    printf("未找到该员工的信息！\n");
}

/************************************************操作用户函数************************************************************/
// 管理员操作
void Administrator_operate(const char *filename)
{
    int chioce;
    // 员工信息链表
    NODE *pStaffList = (NODE *)malloc(sizeof(NODE));
    pStaffList->shuju = NULL;
    pStaffList->xiayige = NULL;
    LoadStaffToList(pStaffList, filename);
    // PrintStaffList(pStaffList);//测试函数,测试文件内容是否加载到链表
    printf("员工信息加载完成。\n");
    while (1)
    {
        Administrator("../ui/administrator");
        printf("输入你的选择：");
        scanf("%d", &chioce);
        switch (chioce)
        {
        case 1:
            Change_message(pStaffList, filename); // 修改员工信息
            break;
        case 2:
            Add_message(pStaffList, filename); // 添加员工信息
            break;
        case 3:
            Delete_message(pStaffList, filename); // 删除员工
            break;
        case 4:
            Show_message(pStaffList); // 显示所有员工信息
            break;
        case 5:
            Show_person(pStaffList); // 显示个人所有信息
            break;
        case 6:
            Approve_apply(pStaffList, filename); // 更改员工的部门根据员工申请的部门，并且把申请的部门一栏用“空”字表示已经更换
            break;
        case 7:
            SaveAllStaff(pStaffList, filename); // 退出前保存所有员工信息
            free(pStaffList);                   // 退出前释放链表
            exit(0);
            break;
        default:
            printf("输入的选择错误，请重新输入\n");
            break;
        }
    }
}
// 普通员工操作
void Staff_operate(const char *filename)
{
    int chioce;
    // 员工信息链表
    NODE *pStaffList = (NODE *)malloc(sizeof(NODE));
    pStaffList->shuju = NULL;
    pStaffList->xiayige = NULL;
    LoadStaffToList(pStaffList, filename);
    // PrintStaffList(pStaffList);//测试函数,测试文件内容是否加载到链表
    printf("员工信息加载完成。\n");
    while (1)
    {
        Staff("../ui/staff");
        printf("输入你的选择：");
        scanf("%d", &chioce);
        switch (chioce)
        {
        case 1:
            Show_salary(pStaffList); // 查看工资
            break;
        case 2:
            Show_department(pStaffList); // 查看所在部门
            break;
        case 3:
            Show_person(pStaffList); // 显示个人所有信息
            break;
        case 4:
            Apply_change_Dp(pStaffList, filename); // 申请更换部门，初始为"空"字字符串,申请后就更换成要申请的部门字符串
            break;
        case 5:
            SaveAllStaff(pStaffList, filename); // 退出前保存所有员工信息
            free(pStaffList);                   // 退出前释放链表
            exit(0);
            break;
        default:
            printf("输入的选择错误，请重新输入\n");
            break;
        }
    }
}

/************************************************测试函数************************************************************/

// 检测文件内容是否加载到链表
/* void PrintStaffList(NODE *pStaffList)
{
    NODE *current = pStaffList->xiayige; // 从链表的第一个有效节点开始
    while (current != NULL)
    {
        STAFF *staff = (STAFF *)current->shuju;
        printf("姓名：%s, 性别：%s, 年龄：%s, 薪水：%s, 部门：%s, 申请更换的部门：%s\n",
               staff->name, staff->sex, staff->age, staff->salary, staff->department, staff->change);
        current = current->xiayige;
    }
} */